<template>
  <div v-bind="$attrs" ref="$chartDom" />
</template>

<script>
export default {
  name: "VolCurChart"
}
</script>

<script setup>
import {ref, watch, computed, defineProps, onBeforeUnmount} from 'vue'
import * as echarts from 'echarts'
import elementResizeDetectorMaker from 'element-resize-detector'


const erd = elementResizeDetectorMaker()
const props = defineProps({
  data: {
    type: Array
  }  // data[{time: Date.now(), vol: 0.00, cur: 0.00}]
})
const data = computed(() => props.data)
const $chartDom = ref(null)
let chart = null
let volData = []
let curData = []
const chartUpdate = () => {
  volData = []
  curData = []
  data.value.forEach((val) => {
    volData.push([val.time, val.vol])
    curData.push([val.time, val.cur])
  })
  chart.setOption({
    series: [
      { data: volData },
      { data: curData }
    ]
  })
}
const option = {
  title: {
    text: 'xxxx供电状态',
    left: 'center',
    top: 10
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      animation: true
    }
  },
  // legend: {
  //   data: ['电压', '电流'],
  //   left: 30,
  //   top: 7
  // },
  toolbox: {
    top: 10,
    right: 30,
    feature: {
      dataZoom: {
        yAxisIndex: 'none'
      },
      restore: {},
      saveAsImage: {}
    }
  },
  axisPointer: {
    link: [
      {
        xAxisIndex: 'all'
      }
    ]
  },
  dataZoom: [
    // {
    //   show: true,
    //   realtime: true,
    //   start: 0,
    //   end: 100,
    //   xAxisIndex: [0, 1]
    // },
    {
      type: 'inside',
      realtime: true,
      start: 0,
      end: 100,
      xAxisIndex: [0, 1]
    }
  ],
  grid: [
    {
      left: 60,
      right: 50,
      height: '39%'
    },
    {
      left: 60,
      right: 50,
      bottom: 80,
      height: '39%'
    }
  ],
  xAxis: [
    {
      type: 'time',
      axisLabel: {
        formatter: '{HH}:{mm}:{ss}', // 得到的 label 形如：'13:12:59.117'
      },
      splitLine: {
        show: false
      }
    },
    {
      gridIndex: 1,
      type: 'time',
      axisLabel: {
        formatter: '{HH}:{mm}:{ss}', // 得到的 label 形如：'13:12:59.117'
      },
      splitLine: {
        show: false
      }
    },
  ],
  yAxis: [
    {
      name: 'V',
      type: 'value',
    },
    {
      gridIndex: 1,
      name: 'A',
      type: 'value',
    }
  ],
  series: [
    {
      name: '电压',
      type: 'line',
      symbol: 'none',
      smooth: true
    },
    {
      xAxisIndex: 1,
      yAxisIndex: 1,
      name: '电流',
      type: 'line',
      symbol: 'none',
      smooth: true
    }
  ]
}
const chartResize = () => {
  chart.resize()
}
watch($chartDom, () => {
  chart = echarts.init($chartDom.value)
  option && chart.setOption(option)
  chartUpdate()
  //监听图表大小变化
  // $chartDom.value.addEventListener('resize', chartResize)  // div绑定resize事件无效，采用第三方插件解决
  erd.listenTo($chartDom.value, chartResize)
})
onBeforeUnmount(() => {
  chart.dispose()
  erd.removeListener(chartResize)
})
watch(data, () => {
  chartUpdate()
})

</script>

<style scoped>

</style>
